package co.fitstart.coach.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import co.fitstart.coach.entity.security.Login;
import co.fitstart.coach.service.CourseService;
import co.fitstart.coach.service.PrincipalService;
import co.fitstart.coach.service.StudentService;
import co.fitstart.coach.web.resolver.Secure;
import co.fitstart.entity.coach.CoachStudentRelation;
import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.user.Student;


/**
 * 
 * @author lidan
 *
 */
@Controller
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private PrincipalService principalService;
    
    @Autowired
    private StudentService studentService;
    
    @Autowired
    private CourseService courseService;
    
    /**
     * List student by type
     * 
     * @param login 
     * @param type
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(@Secure Login login, @RequestParam(value = "type", required = false, defaultValue = "individual") String type){
        
        ModelAndView modelAndView = new ModelAndView("student/student-list");
        
        Principal principal = principalService.load(Principal.class, login.getId());
        
        CourseType courseType = CourseType.aliasOf(type);
        List<CoachStudentRelation> coachStudentRelations = studentService.listStudent(principal, courseType);
        
        modelAndView.addObject("courseType", courseType);
        modelAndView.addObject("coachStudentRelations", coachStudentRelations);
        
        return modelAndView;
    }
    
    /**
     * View student by studentId
     * 
     * @param login
     * @param studentId
     * @param lastCourseScheduleId
     * @param type
     * @return
     */
    @RequestMapping(value = "/{studentId}", method = RequestMethod.GET)
    public ModelAndView view(@Secure Login login, 
                             @PathVariable("studentId") Long studentId,
                             @RequestParam(value = "type", required = false, defaultValue = "individual") String type){
        
        ModelAndView modelAndView = new ModelAndView("student/student-view");
        
        // load coach
        Principal principal = principalService.load(Principal.class, login.getId());
        
        // load student and his course round
        Student student = studentService.load(Student.class, studentId);
        
        CourseType courseType = CourseType.aliasOf(type);
        
        List<CourseRound> courseRounds = courseService.listCourseRound(student, courseType);
        
        
        modelAndView.addObject("principal", principal);
        modelAndView.addObject("student", student);
        
        modelAndView.addObject("courseRounds", courseRounds);
        
        return modelAndView;
    
    }
    
    /**
     * 
     * @param login
     * @param studentId
     * @param roundId
     * @return
     */
    @RequestMapping(value = "/{studentId}/rounds/{roundId}", method = RequestMethod.GET)
    public ModelAndView courseView(@Secure Login login,
                                   @PathVariable("studentId")Long studentId,
                                   @PathVariable("roundId") Long roundId,
                                   @RequestParam(value="scheduleId", required=false) Long scheduleId) {
        
        ModelAndView modelAndView = new ModelAndView("student/student-course-view");
        
        // load coach
        Principal principal = principalService.load(Principal.class, login.getId());
        
        // load course round
        CourseRound courseRound = courseService.loadCourseRound(roundId);
        
        if(scheduleId != null) {
            CourseSchedule schedule = courseService.loadCourseSchedule(scheduleId);
            
            modelAndView.addObject("schedule", schedule);
        }
        
        modelAndView.addObject("coach", principal);
        modelAndView.addObject("student", courseRound.getStudent());
        modelAndView.addObject("courseRound", courseRound);
        modelAndView.addObject("courseSchedules", courseRound.getSchedules());
        
        return modelAndView;
    }
}
